//
//  RCNewFeatureCollectionViewController.swift
//  RCWeibo
//
//  Created by CL on 15/12/17.
//  Copyright © 2015年 CL. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"

class RCNewFeatureCollectionViewController: UICollectionViewController {
    
    //做UICollectionViewController的layout
    init(){
        
        //创建layoutxxxxxxxxxx
        let layout = UICollectionViewFlowLayout()
        //大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //滚动方式
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //⚠️这个self什么意思oc中是［RCNewfeatureCell class］
        self.collectionView!.registerClass(RCNewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    // MARK: UICollectionViewDataSource
    
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! RCNewfeatureCell
        
        // Configure the cell
        //        cell.backgroundColor = indexPath.row%2 == 0 ? UIColor.redColor() : UIColor.yellowColor()
        
        cell.index = indexPath.item
        
        
        return cell
    }
    
    //当cell展示完后,indexpath是消失的那个cell，不能直接用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //        print(indexPath)
        
        //获取当前展示的cell
        let cell = collectionView.visibleCells().last as! RCNewfeatureCell
        
        
        if cell.index == 3 {
            
            //执行动画
            cell.showAnimation()
            
        }
    }
    
    
    
}

//统一文件创建第二个class，因为cell只有在这里用到

class RCNewfeatureCell: UICollectionViewCell{
    
    //设置属性
    var index: Int = 0 {
        didSet{
            
            //设置每个到图片
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            
            
            startBtn.hidden = index != 3
            
        }
    }
    
    //btn点击事件
    @objc private func startBtnDidClick(){
        
        print("startBtnDidClick")
        let obj = RCUserAccountViewModel().isLogin ? "" : "nologin"
        defaultNotification.postNotificationName(WBSwitchRootVCNote, object: obj)

        
    }
    // btn动画
    private func showAnimation(){
        
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        /**
        衰变的动画效果
        iOS7.0推出的
        
        - parameter <Tduration:             动画持续时间
        - parameter delay:                  动画延迟时间
        - parameter usingSpringWithDamping: 弹簧系数 区间 0.1 ~ 1  越小越弹
        - parameter initialSpringVelocity:  加速度
        - parameter options:                动画的可选项  swift中使用数组
        - parameter animations:             执行动画闭包
        - parameter completion:             动画执行完成的闭包
        */
        
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
                print("动画完成")
        }
        
        
    }
    
    
    //重写构造方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    func setupUI(){
        //        自定义到视图添加在cell的contentView上
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        
        //fram不对,设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView.snp_edges)
            
            
        }
        //btn约束
        startBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(contentView.snp_centerX)
            
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
            
            
        }
        
        
    }
    //MARK:
    //MARK: //懒加载

    //image
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    //btn
    private lazy var startBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitle("开始体验", forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        
        btn.sizeToFit()
        
        //给btn添加点击事件
        
        btn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
        
        return btn
    }()
    
    
}
